// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
  "name": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      },
      {
        "maxLength": 50
      }
    ],
    "title": "家具名称",
    "label": "家具名称"
  },
  "category": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      },
      {
        "range": [
          {
            "value": "沙发",
            "text": "沙发"
          },
          {
            "value": "橱柜",
            "text": "橱柜"
          },
          {
            "value": "屏风",
            "text": "屏风"
          },
          {
            "value": "架类",
            "text": "架类"
          },
          {
            "value": "桌椅",
            "text": "桌椅"
          },
          {
            "value": "床具",
            "text": "床具"
          }
        ]
      }
    ],
    "title": "分类",
    "label": "分类"
  },
  "price": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "double"
      },
      {
        "minimum": 0
      }
    ],
    "title": "价格",
    "label": "价格"
  },
  "sales": {
    "rules": [
      {
        "format": "int"
      }
    ],
    "title": "销量",
    "defaultValue": 0,
    "label": "销量"
  },
  "description": {
    "rules": [
      {
        "format": "string"
      },
      {
        "maxLength": 500
      }
    ],
    "title": "描述",
    "label": "描述"
  },
  "images": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "array"
      }
    ],
    "title": "图片列表",
    "label": "图片列表"
  },
  "material": {
    "rules": [
      {
        "format": "string"
      },
      {
        "maxLength": 20
      }
    ],
    "title": "材质",
    "label": "材质"
  },
  "characteristics": {
    "rules": [
      {
        "format": "array"
      }
    ],
    "title": "特征标签",
    "label": "特征标签"
  },
  "tags": {
    "rules": [
      {
        "format": "array"
      }
    ],
    "title": "分类标签",
    "label": "分类标签"
  },
  "create_time": {
    "rules": [
      {
        "format": "timestamp"
      }
    ],
    "title": "创建时间",
    "defaultValue": {
      "$env": "now"
    },
    "label": "创建时间"
  }
}

const enumConverter = {
  "category_valuetotext": {
    "沙发": "沙发",
    "橱柜": "橱柜",
    "屏风": "屏风",
    "架类": "架类",
    "桌椅": "桌椅",
    "床具": "床具"
  }
}

function filterToWhere(filter, command) {
  let where = {}
  for (let field in filter) {
    let { type, value } = filter[field]
    switch (type) {
      case "search":
        if (typeof value === 'string' && value.length) {
          where[field] = new RegExp(value)
        }
        break;
      case "select":
        if (value.length) {
          let selectValue = []
          for (let s of value) {
            selectValue.push(command.eq(s))
          }
          where[field] = command.or(selectValue)
        }
        break;
      case "range":
        if (value.length) {
          let gt = value[0]
          let lt = value[1]
          where[field] = command.and([command.gte(gt), command.lte(lt)])
        }
        break;
      case "date":
        if (value.length) {
          let [s, e] = value
          let startDate = new Date(s)
          let endDate = new Date(e)
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
      case "timestamp":
        if (value.length) {
          let [startDate, endDate] = value
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
    }
  }
  return where
}

export { validator, enumConverter, filterToWhere }
